package com.seventh.parkingrent.controller;


import com.seventh.parkingrent.commens.Result;
import com.seventh.parkingrent.controller.form.ManagerForm;
import com.seventh.parkingrent.domain.Manager;
import com.seventh.parkingrent.service.IManagerService;
import com.seventh.parkingrent.service.IParkingSpaceService;
import com.seventh.parkingrent.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-08-25
 */
@RestController
@Slf4j
@CrossOrigin("*")
public class ManagerController {
    @Resource
    private IManagerService managerService;

    @Resource
    private IUserService userService;

    @Resource
    private IParkingSpaceService parkingSpaceService;

    @GetMapping("/getManagerName")
    public Result getManagerName(){
        log.info("前端提请获取管理人员名字");
        List<String> managerNames = managerService.getManagerName();

        return Result.success(managerNames);
    }

    @PostMapping("/login")
    public Result login(@Valid @RequestBody ManagerForm form, BindingResult bindingResult) throws NoSuchAlgorithmException {
        log.info("获取管理人员登录数据form={}",form);

        //调用本类共同方法，当存在自动检验问题时，直接返回Result.fail
        if (bindingResult.hasErrors()) {
            return getBindingResult(bindingResult);
        }

        Manager manager = managerService.login(form);
        //判断是否找到对应manager
        if (null==manager) {
            return Result.fail("账户或密码错误");
        }
        //判断账户是否还有效
        if (!manager.getStatus()) {
            return Result.fail("10087","账户已失效");
        }
        //修改最后登录时间以及登录次数。
        manager.setLoginCount(manager.getManagerId()+1);
        manager.setLastTime(new Date());
        managerService.update(manager);
        return Result.success(manager);
    }

    @PostMapping("/register")
    public Result register(@Valid @RequestBody ManagerForm form, BindingResult bindingResult) throws NoSuchAlgorithmException {
        log.info("获取管理人员注册数据form={}",form);

        //调用本类共同方法，当存在自动检验问题时，直接返回Result.fail
        if (bindingResult.hasErrors()) {
            return getBindingResult(bindingResult);
        }
        //若返回值为true，则表示插入表格成功，即注册成功
        if (managerService.register(form)) {
            return Result.success();
        }

        return Result.fail("该账户名已存在");
    }

    public Result getBindingResult(BindingResult bindingResult){
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        StringBuilder stringBuilder = new StringBuilder();
        for (FieldError fieldError : fieldErrors) {
            stringBuilder.append(fieldError.getField()+"："+fieldError.getDefaultMessage()+";");
        }
        return Result.fail("10086",stringBuilder.toString());
    }


}

